package com.banzhi.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.ChoiceOrderDetailsActivity;
import com.banzhi.shop.activity.ChoiceShouhouOrderInfoActivity;
import com.banzhi.shop.activity.ChoicesDetailsActivity;
import com.banzhi.shop.activity.CommodityDetailsActivity;
import com.banzhi.shop.activity.CommodityDetails_DZZQActivity;
import com.banzhi.shop.activity.OrderDetailsActivity;
import com.banzhi.shop.activity.ShouhouOrderInfoActivity;
import com.banzhi.shop.entity.OrderManagement.OrderInfo_data;
import com.banzhi.shop.entity.OrderManagement.Order_Data;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/11/12.
 */

public class ChoiceOrderManagementAdapter  extends BaseAdapter<Order_Data>{
    private Context context;

    public ChoiceOrderManagementAdapter(Context context, List<Order_Data> data) {
        super(context, data);
        this.context = context;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Order_Data data = getData().get(position);
        ChoiceOrderManagementAdapter.viewHolder holder = null;
        if (convertView == null) {
            holder = new ChoiceOrderManagementAdapter.viewHolder();
            convertView = getInflater().inflate(R.layout.item_order_management, null);
            holder.tvtime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tvzongjia = (TextView) convertView.findViewById(R.id.tv_zongjia);
            holder.tvyunfei = (TextView) convertView.findViewById(R.id.tv_yunfei);
            holder.ivmai = (ImageView) convertView.findViewById(R.id.iv_mai);
            holder.tvxiangqing = (TextView) convertView.findViewById(R.id.tv_xiangqing);
            holder.tvdelete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.tvstatus = (TextView) convertView.findViewById(R.id.tv_statu);
            holder.tvzhifu = (TextView) convertView.findViewById(R.id.tv_zhifu);
            holder.tv_shouhuo = (TextView) convertView.findViewById(R.id.tv_shouhuo);
            holder.tvtuikuan = (TextView) convertView.findViewById(R.id.tv_tuikuan);
            holder.tvdeleteorder = (TextView) convertView.findViewById(R.id.tv_delete_order);

            holder.recyclerView = (RecyclerView) convertView.findViewById(R.id.recyclerview);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setSmoothScrollbarEnabled(true);
            layoutManager.setAutoMeasureEnabled(true);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            holder.recyclerView.setLayoutManager(layoutManager);
            holder.recyclerView.setHasFixedSize(true);
            holder.recyclerView.setNestedScrollingEnabled(false);
            convertView.setTag(holder);
        }
        holder = (ChoiceOrderManagementAdapter.viewHolder) convertView.getTag();
        if (ASUtils.isCanUse(data.getCreatetime())) {
            holder.tvtime.setText(DateUtil.getTimeZhiData(data.getCreatetime()));
        }
        if (ASUtils.isCanUse(data.getPrice())) {
            holder.tvzongjia.setText(NumberUtils.DecimalFormat(data.getPrice()));
        }
        if (ASUtils.isCanUse(data.getDispatchprice())) {
            holder.tvyunfei.setText(NumberUtils.DecimalFormat(data.getDispatchprice()));
        }
        if ("1".equals(data.getShoperOrder())) {
            holder.ivmai.setImageResource(R.mipmap.mai);
        } else {
            holder.ivmai.setImageResource(R.mipmap.mai1);
        }
        if ("0".equals(data.getStatus())) {
            holder.tvstatus.setText("待付款");
            if (!"1".equals(data.getShoperOrder())) {
                holder.tvdelete.setVisibility(View.GONE);
                holder.tvzhifu.setVisibility(View.GONE);
                holder.tvdeleteorder.setVisibility(View.GONE);
            } else {
                holder.tvdelete.setVisibility(View.VISIBLE);
                holder.tvzhifu.setVisibility(View.VISIBLE);
                holder.tvdeleteorder.setVisibility(View.VISIBLE);
            }
            holder.tv_shouhuo.setVisibility(View.GONE);
            holder.tvtuikuan.setVisibility(View.GONE);
        } else if ("1".equals(data.getStatus())) {
            holder.tvstatus.setText("待发货");
            holder.tvdelete.setVisibility(View.GONE);
            holder.tvzhifu.setVisibility(View.GONE);
            holder.tv_shouhuo.setVisibility(View.GONE);
            holder.tvdeleteorder.setVisibility(View.GONE);
            if (!"1".equals(data.getShoperOrder())) {
                holder.tvtuikuan.setVisibility(View.GONE);
            } else {
                if (Integer.parseInt(data.getRefundstate()) > 0) {
                    holder.tvtuikuan.setVisibility(View.GONE);
                } else {
                    holder.tvtuikuan.setVisibility(View.VISIBLE);
                }
            }
            if (Integer.parseInt(data.getRefundstate()) > 0) {
                holder.tvstatus.setText("售后维权中");
            } else {
                holder.tvstatus.setText("待发货");
            }
        } else if ("2".equals(data.getStatus())) {
            holder.tvstatus.setText("待收货");
            holder.tvdelete.setVisibility(View.GONE);
            holder.tvzhifu.setVisibility(View.GONE);
            holder.tvdeleteorder.setVisibility(View.GONE);
            holder.tvtuikuan.setVisibility(View.VISIBLE);
            if (!"1".equals(data.getShoperOrder())) {
                holder.tv_shouhuo.setVisibility(View.GONE);
            } else {
                holder.tv_shouhuo.setVisibility(View.VISIBLE);
            }
        } else if ("3".equals(data.getStatus())) {
            holder.tvstatus.setText("已完成");
            holder.tvdelete.setVisibility(View.GONE);
            holder.tvzhifu.setVisibility(View.GONE);
            holder.tv_shouhuo.setVisibility(View.GONE);
            if (!"1".equals(data.getShoperOrder())) {
                holder.tvtuikuan.setVisibility(View.GONE);
                holder.tvdeleteorder.setVisibility(View.GONE);
            } else {
                holder.tvtuikuan.setVisibility(View.VISIBLE);
                if (Integer.parseInt(data.getRefundstate()) > 0) {
                    holder.tvtuikuan.setVisibility(View.GONE);
                    holder.tvdeleteorder.setVisibility(View.GONE);
                } else {
                    holder.tvtuikuan.setVisibility(View.VISIBLE);
                    holder.tvdeleteorder.setVisibility(View.VISIBLE);
                }
            }
            if (ASUtils.isCanUse(data.getRefundstate()) && Integer.parseInt(data.getRefundstate()) > 0) {
                holder.tvstatus.setText("售后维权中");
            } else {
                holder.tvstatus.setText("已完成");
            }
        } else if ("-1".equals(data.getStatus())) {
            holder.tvstatus.setText("已取消");
            holder.tvdelete.setVisibility(View.VISIBLE);
            holder.tvzhifu.setVisibility(View.GONE);
            holder.tvxiangqing.setVisibility(View.VISIBLE);
        }
        holder.tvxiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(data.getRefundstate()) > 0) {
                  //  Intent intent = new Intent(getContext(), ShouhouOrderInfoActivity.class);
                    Intent intent = new Intent(getContext(), ChoiceShouhouOrderInfoActivity.class);
                    intent.putExtra("id", data.getRefundid());
                    intent.putExtra("shoperOrder", data.getShoperOrder());
                    getContext().startActivity(intent);
                } else {
                //    Intent osintent = new Intent(getContext(), OrderDetailsActivity.class);
                    Intent osintent = new Intent(getContext(), ChoiceOrderDetailsActivity.class);
                    osintent.putExtra("id", data.getId());
                    osintent.putExtra("status", data.getStatus());
                    osintent.putExtra("shoperOrder",data.getShoperOrder());
                    getContext().startActivity(osintent);
                }
            }
        });
        holder.tvdelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showquxiaoPopupwindow(data.getId(), position);

            }
        });
        holder.tvzhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.zhifu(data.getId(), position);
            }
        });
        holder.tv_shouhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showquerenPopupwindow(data.getId(), position);
            }
        });
        holder.tvtuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.tuikuan(data.getId(), position);
            }
        });
        holder.tvdeleteorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupwindow(data.getId(), position);
            }
        });
        CommonAdapter<OrderInfo_data> adapter = new CommonAdapter<OrderInfo_data>(context, R.layout.item_order_management_shangping, data.getOrderGoods()) {
            @Override
            public void convert(ViewHolder holder, OrderInfo_data orderInfodata, int position) {
                if (ASUtils.isCanUse(orderInfodata.getThumb())) {
                    holder.setImageURI(R.id.iv_image, orderInfodata.getThumb());
                } else {
                    holder.setImageURI(R.id.iv_image, R.mipmap.moren);
                }
                holder.setText(R.id.tv_title, orderInfodata.getTitle());
                if (orderInfodata.getOptionname() != null && !"".equals(orderInfodata.getOptionname())) {
                    holder.getTextView(R.id.tv_text_guige).setVisibility(View.VISIBLE);
                    holder.setText(R.id.tv_guige, orderInfodata.getOptionname());
                } else {
                    holder.getTextView(R.id.tv_text_guige).setVisibility(View.GONE);
                }
                if (ASUtils.isCanUse(orderInfodata.getPrice())) {
                    holder.setText(R.id.tv_danjia, NumberUtils.DecimalFormat(orderInfodata.getPrice()));
                }
                holder.setText(R.id.tv_num, orderInfodata.getTotal());
                if (orderInfodata.getCommission1() != null && !"".equals(orderInfodata.getCommission1())) {
                    holder.setText(R.id.tv_commission, NumberUtils.DecimalFormat(orderInfodata.getCommission1()));
                }
                if ("1".equals(data.getShoperOrder())) {
                    holder.getImageView(R.id.iv_shen_zhuan).setImageResource(R.mipmap.sheng);
                } else {
                    holder.getImageView(R.id.iv_shen_zhuan).setImageResource(R.mipmap.zhuan);
                }

            }
        };
        holder.recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if ("1".equals(data.getOrderGoods().get(position).getIsregistergoods())) {
                    ToastUtils.makeToastShort("该商品为注册商品");
                } else if ("0".equals(data.getOrderGoods().get(position).getStatus())) {
                    ToastUtils.makeToastShort("该商品已下架");
                } else if ("1".equals(data.getOrderGoods().get(position).getDeleted())) {
                    ToastUtils.makeToastShort("该商品已删除");
                } else {
                    Intent intent = null;
                    if ("1".equals(data.getOrderGoods().get(position).getIsagentgoods())) {
                        intent = new Intent(getContext(), CommodityDetails_DZZQActivity.class);
                    } else {
                        intent = new Intent(getContext(), ChoicesDetailsActivity.class);
                    }
                    intent.putExtra("id", data.getOrderGoods().get(position).getGoodsid());
                    getContext().startActivity(intent);
                }
            }
        });

        return convertView;
    }

    class viewHolder {
        TextView tvtime;
        ImageView ivmai;
        TextView tvzongjia;
        TextView tvyunfei;
        TextView tvxiangqing;
        TextView tvdelete;
        TextView tvzhifu;
        TextView tvstatus;
        TextView tv_shouhuo;
        TextView tvtuikuan;
        TextView tvdeleteorder;
        RecyclerView recyclerView;
    }

    public interface zhifuCallBack {
        void zhifu(String id, int position);

        void delete(String id, int position);

        void queren(String id, int position);

        void tuikuan(String id, int position);

        void deleteorder(String id, int position);
    }

    private ChoiceOrderManagementAdapter.zhifuCallBack callBack;

    public void getcallback(ChoiceOrderManagementAdapter.zhifuCallBack callBack) {
        this.callBack = callBack;
    }


    /**
     * 点击删除提示下是不是确定删除
     *
     * @param id
     * @param position
     */
    private void showPopupwindow(final String id, final int position) {
        //设置contentView
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_delete_order, null);

        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        int height = getContext().getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.deleteorder(id, position);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    /**
     * 点击确认收货是不是确定确认收货
     *
     * @param id
     * @param position
     */
    private void showquerenPopupwindow(final String id, final int position) {
        //设置contentView
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_queren_shouhuo, null);

        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        int height = getContext().getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.queren(id, position);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    /**
     * 点击取消订单  确定是不是确定取消订单
     *
     * @param id
     * @param position
     */
    private void showquxiaoPopupwindow(final String id, final int position) {
        //设置contentView
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_cancel_order, null);

        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        int height = getContext().getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.delete(id, position);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
